﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AutoTestEntry.SpecificUtility;

namespace AutoTestEntry.UI
{
    public partial class FtpConfigForm : Form
    {
		private EnvDepoly envDeploy;
		private bool check;
        public FtpConfigForm(bool check)
        {
            InitializeComponent();
            Init();

			this.check = check;
        }

        private void Init()
        {
			lblState.Visible = false;

            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
			this.Shown += Form_Shown;
            
            btnOK.Click += btnOK_Click;
            btnCancel.Click += btnCancel_Click;

			envDeploy = new EnvDepoly();

			// init the backgroundworker 
			bgCheckFtpWorker.DoWork += bgCheckFtpWorker_DoWork;
			bgCheckFtpWorker.RunWorkerCompleted += bgCheckFtpWorker_RunWokerCompleted;
        }

		private void Form_Shown(Object sender, EventArgs e)
		{
			txtFtpAddress.Text = envDeploy.GetFtpAddress();
            txtUsername.Text = envDeploy.GetFtpUserName();
            txtPasswd.Text = envDeploy.GetFtpPasswd();

			if (txtFtpAddress.Text.Length > 0 && check)
			{
				btnOK_Click(this, null);
			}
		}

        private void btnOK_Click(Object sender, EventArgs e)
        {
            if (IsLegalIPAddress(txtFtpAddress.Text))
            {
				lblState.Visible = true;
				txtFtpAddress.Enabled = false;
				btnOK.Enabled = false;

                envDeploy.SetFtpAdress("ftp://" + txtUsername.Text + ":" + txtPasswd.Text + "@" + txtFtpAddress.Text);
                

				bgCheckFtpWorker.RunWorkerAsync();
            } 
            else
            {
                MessageBox.Show("非法IP地址，请重新输入");
                txtFtpAddress.Clear();
            }
        }

        private void btnCancel_Click(Object sender, EventArgs e)
        {
			this.DialogResult = DialogResult.Cancel;
			this.Dispose();
        }

        private bool IsLegalIPAddress(String ip)
        {
            String[] segs = ip.Split(new String[]{"."}, StringSplitOptions.RemoveEmptyEntries);

            if (segs.Length == 4)
            {
                for (int i = 0; i < 4; ++i)
                {
                    try
                    {
                        int num = Convert.ToInt32(segs[i]);
                        if (num <0 || num > 255)
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

		private void bgCheckFtpWorker_DoWork(Object sender, DoWorkEventArgs e)
		{
			e.Result = EnvInitializer.CheckForNewVersion();
		}

		private void bgCheckFtpWorker_RunWokerCompleted(Object sender, RunWorkerCompletedEventArgs e)
		{
			lblState.Visible = false;
			txtFtpAddress.Enabled = true;
			btnOK.Enabled = true;

			if (e.Error != null)
			{
				MessageBox.Show(e.Error.Message);
			}
			else if (e.Cancelled)
			{
				//TODO 
				//MessageBox.Show("Cancelled");
			}
			else if (e.Result == null)	// incorrect ftp address
			{
				MessageBox.Show("Ftp 地址或者密码不正确");
			}
			else
			{
				this.DialogResult = DialogResult.Yes;
				this.Dispose();
			}
		}
    }
}
